package com.Yukiha.gachamod.network;

import net.minecraft.resources.ResourceLocation;
import net.minecraftforge.network.NetworkRegistry;
import net.minecraftforge.network.PacketDistributor;
import com.Yukiha.gachamod.GachaMod;
import net.minecraft.server.level.ServerPlayer;
import net.minecraftforge.network.simple.SimpleChannel;

public class ModNetworkHandler {

    private static final String PROTOCOL_VERSION = "1.0";

    public static final SimpleChannel CHANNEL = NetworkRegistry.newSimpleChannel(
            new ResourceLocation(GachaMod.MODID, "main"), // eg: "gachamod:main"
            () -> PROTOCOL_VERSION,
            PROTOCOL_VERSION::equals,
            PROTOCOL_VERSION::equals
    );

    private static int packetId = 0;

    public static void register() {
        CHANNEL.registerMessage(packetId++,
                ClientboundSyncGachaDataPacket.class,
                ClientboundSyncGachaDataPacket::encode,
                ClientboundSyncGachaDataPacket::decode,
                ClientboundSyncGachaDataPacket::handle);
    }

    /** 发给指定玩家（常用） */
    public static void sendTo(ServerPlayer player, Object packet) {
        CHANNEL.send(PacketDistributor.PLAYER.with(() -> player), packet);
    }
}
